package bom.example.common.quarzt.config;


import bom.example.common.quarzt.domain.SysJob;
import bom.example.common.utils.ReflectUtils;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/3/20 20:00
 * @since JDK 1.8
 *
 * 定时执行某个方法
 */
@Slf4j
public class MethodJob extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JobDetail jobDetail=jobExecutionContext.getJobDetail();
        JobDataMap jobDataMap=jobDetail.getJobDataMap();
        SysJob sysJob =(SysJob) jobDataMap.get("sysjob");
        try {
            if("1".equals(sysJob.getTypeCode().toString())){
                String clazz= sysJob.getMethodClazz();
                String methodname= sysJob.getMethodName();
                JSONArray jsonArray= JSONArray.parseArray(sysJob.getParam());
                Object [] args=new Object[jsonArray.size()] ;
                for (int i = 0; i < jsonArray.size(); i++) {
                    args[i]=jsonArray.get(i);
                }
                System.out.println(args.length+"length");
                Object value= ReflectUtils.methodInvoke(clazz,methodname,args);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
